package cmd

import (
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

func genCmdLine() *cobra.Command {
	cmdLine := &cobra.Command{
		Use:   "cmdline",
		Short: "run server by cmdline.",
		Long:  `run server by cmdline.`,
		PersistentPreRun: func(cmd *cobra.Command, args []string) {
			setup()
		},
	}

	//cmdLine.AddCommand(genRunCmd())
	cmdLine.AddCommand(genDryRunCmd())
	cmdLine.AddCommand(genRemoveCmd())
	cmdLine.AddCommand(genLinkCmd())

	cmdLine.PersistentFlags().StringVarP(&timeType, "time-type", "t", "", "choose: atime, mtime, ctime")
	cmdLine.PersistentFlags().StringVarP(&timeAfter, "time-after", "a", "", "file filter by time After this, Format: YYYY-MM-DD hh24:mi:ss, (default 1970-01-01 00:00:00)")
	cmdLine.PersistentFlags().StringVarP(&timeBefore, "time-before", "b", "", "file filter by time Before this, Format: YYYY-MM-DD hh24:mi:ss, (default localtime)")
	cmdLine.PersistentFlags().StringVarP(&path, "path", "p", "", "input the path of duplication")

	cmdLine.PersistentFlags().BoolVarP(&cascade, "cascade", "d", true, "whether the cascade for deletion")
	cmdLine.PersistentFlags().IntVarP(&goroutine, "goroutine", "g", 100, "the concurrency number to deal with file")
	// 必须参数
	_ = cmdLine.MarkPersistentFlagRequired("path")
	// 命令添加viper文件参数读取绑定
	_ = viper.BindPFlags(cmdLine.PersistentFlags())

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// serverCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

	return cmdLine
}

// setup 检查配置信息参数
func setup() {
	if timeType != "atime" && timeType != "ctime" && timeType != "mtime" && timeType != "" {
		panic("timeType must in atime, ctime, mtime")
	}
	if timeType != "" && timeAfter == "" && timeBefore == "" {
		panic("timeAfter or timeBefore must specified when timeType is used")
	}
}
